package com.sky.demo.activities;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sky.demo.R;
import com.sky.demo.fragment.One;
import com.sky.demo.fragment.Three;
import com.sky.demo.fragment.Two;
import com.sky.demo.global.BaseActivity;
import com.sky.demo.utils.LogUtils;
import com.sky.demo.views.BadgeView;

/**
 * @ClassName: ViewPagerActivity
 * @Description:  仿微信主界面
 * @author sky
 * @date 2015年4月15日 下午4:15:21
 * 
 */
public class ViewPagerActivity extends BaseActivity {

	private LinearLayout id_tab_liaotian_ly;
	private LinearLayout id_tab_faxian_ly;
	private LinearLayout id_tab_tongxunlu_ly;
	private TextView id_liaotian;
	private TextView id_faxian;
	private TextView id_tongxunlu;
	private ImageView id_tab_line;

	private ViewPager viewPager;
	private FragmentPagerAdapter adapter;
	List<Fragment> fragment;

	int screenWidth, screenHeight;
	int lastX, lastY;

	int currentItem;
	BadgeView badgeView;
	LinearLayout linear;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_viewpager);
		id_tab_line = (ImageView) findViewById(R.id.id_tab_line);
		DisplayMetrics outMetrics = new DisplayMetrics();
		getWindow().getWindowManager().getDefaultDisplay()
				.getMetrics(outMetrics);
		screenWidth = outMetrics.widthPixels;
		screenHeight = outMetrics.heightPixels;
		LayoutParams layoutParams = id_tab_line.getLayoutParams();
		layoutParams.width = screenWidth / 3;
		id_tab_line.setLayoutParams(layoutParams);

		badgeView = new BadgeView(this);
		// badgeView = (BadgeView) findViewById(android.R.id.button1);
		linear = (LinearLayout) findViewById(android.R.id.background);
		badgeView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// badgeView.setBadgeCount(100);
			}
		});
		badgeView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				int ea = event.getAction();
				Log.i("TAG", "Touch:" + ea);
				switch (ea) {
				case MotionEvent.ACTION_DOWN:
					lastX = (int) event.getRawX();// 获取触摸事件触摸位置的原始X坐标
					lastY = (int) event.getRawY();
					break;

				case MotionEvent.ACTION_MOVE:
					int dx = (int) event.getRawX() - lastX;
					int dy = (int) event.getRawY() - lastY;

					int l = v.getLeft() + dx;
					int b = v.getBottom() + dy;
					int r = v.getRight() + dx;
					int t = v.getTop() + dy;

					// 下面判断移动是否超出屏幕
					if (l < 0) {
						l = 0;
						r = l + v.getWidth();
					}

					if (t < 0) {
						t = 0;
						b = t + v.getHeight();
					}

					if (r > screenWidth) {
						r = screenWidth;
						l = r - v.getWidth();
					}

					if (b > screenHeight) {
						b = screenHeight;
						t = b - v.getHeight();
					}
					v.layout(l, t, r, b);

					lastX = (int) event.getRawX();
					lastY = (int) event.getRawY();
//					LogUtils.i("当前位置：" + l + "," + t + "," + r + "," + b);
					v.postInvalidate();
					break;
				case MotionEvent.ACTION_UP:
					break;
				}
				return false;
			}
		});
		initView();
		initListener();
	}

	private void initView() {
		id_tab_liaotian_ly = (LinearLayout) findViewById(R.id.id_tab_liaotian_ly);
		id_tab_faxian_ly = (LinearLayout) findViewById(R.id.id_tab_faxian_ly);
		id_tab_tongxunlu_ly = (LinearLayout) findViewById(R.id.id_tab_tongxunlu_ly);
		id_liaotian = (TextView) findViewById(R.id.id_liaotian);
		id_faxian = (TextView) findViewById(R.id.id_faxian);
		id_tongxunlu = (TextView) findViewById(R.id.id_tongxunlu);
		id_tab_line = (ImageView) findViewById(R.id.id_tab_line);

		viewPager = (ViewPager) findViewById(R.id.id_viewpager);

		fragment = new ArrayList<Fragment>();

		One one = new One();
		Two two = new Two();
		Three three = new Three();
		fragment.add(one);
		fragment.add(two);
		fragment.add(three);
		adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

			@Override
			public int getCount() {
				return fragment.size();
			}

			@Override
			public Fragment getItem(int arg0) {
				return fragment.get(arg0);
			}
		};
		viewPager.setAdapter(adapter);

	}

	private void initListener() {
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				resetTextView();
				switch (position) {
				case 0:
					id_tab_liaotian_ly.removeView(badgeView);
					badgeView.setBadgeCount(10);
					// badgeView.setVisibility(View.VISIBLE);
					id_tab_liaotian_ly.addView(badgeView);
					id_liaotian.setTextColor(getResources().getColor(
							R.color.green));

					break;
				case 1:
					id_faxian.setTextColor(getResources().getColor(
							R.color.green));

					break;
				case 2:
					id_tongxunlu.setTextColor(getResources().getColor(
							R.color.green));
					break;
				}

				currentItem = position;
			}

			@Override
			public void onPageScrolled(int position, float positionOffset,
					int arg2) {
				LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) id_tab_line
						.getLayoutParams();
				layoutParams.leftMargin = (int) (positionOffset * screenWidth / 3)
						+ position * screenWidth / 3;
				id_tab_line.setLayoutParams(layoutParams);

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
		id_tab_liaotian_ly.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				viewPager.setCurrentItem(0);

			}
		});
		id_tab_faxian_ly.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				viewPager.setCurrentItem(1);

			}
		});
		id_tab_tongxunlu_ly.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				viewPager.setCurrentItem(2);

			}
		});
	}

	protected void resetTextView() {
		id_liaotian.setTextColor(getResources().getColor(R.color.black));
		id_faxian.setTextColor(getResources().getColor(R.color.black));
		id_tongxunlu.setTextColor(getResources().getColor(R.color.black));
	}

}
